package com.zxj.test.controller;

import java.util.Objects;

import javax.validation.Valid;
import javax.validation.constraints.Min;

import com.zxj.test.common.api.Response;
import com.zxj.test.common.exception.EmployeeNotExistException;
import com.zxj.test.common.validation.Groups;
import com.zxj.test.model.Employee;
import com.zxj.test.model.dto.EmployeeDTO;
import com.zxj.test.service.EmployeeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zxj
 * @date 2020/7/18
 */
@RequestMapping("/emp")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/{id}")
    public Response<Employee> getEmp(@PathVariable("id") int id) {
        return Response.ok(employeeService.getEmp(id));
    }

    @PostMapping
    public Response<Void> addEmp(@RequestBody @Validated(Groups.Add.class) EmployeeDTO dto) {
        employeeService.addEmp(Employee.fromDto(dto));
        return Response.ok();
    }

    @PutMapping
    public Response<Void> modifyEmp(@RequestBody @Validated(Groups.Update.class) EmployeeDTO dto) {
        employeeService.modifyEmp(Employee.fromDto(dto));
        return Response.ok();
    }

    @DeleteMapping("/{id}")
    public Response<Void> removeEmp(@PathVariable("id") int id) {
        employeeService.removeEmp(id);
        return Response.ok();
    }
}
